<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="props.title" :useWrapper="true" width="800px" okText="查询">
    <BasicForm @register="registerForm">
      <!--   <template #add="{ field }">
        <Button v-if="Number(field) === 0" @click="add">+</Button>
        <Button v-if="field > 0" @click="del(field)">-</Button>
      </template> -->
      <template #add>
        <div class="search-container">
          <div class="search-left">
            <!-- <a-select :value="pipei" style="width: 92%; margin-bottom: 11px" @change="changeItempin($event)">
              <a-select-option value="all">所有条件都要求匹配</a-select-option>
              <a-select-option value="any">满足任一条件</a-select-option>
            </a-select> -->

            <div class="c-o-d" v-for="(item, index) in connectArr" :key="index">
              <div class="c-o-d-i">
                <div class="c-o-d-i-1" style="width: 30%">
                  <a-select :value="item.linkKey" style="width: 100%" placeholder="请选择查询字段" @change="changeItem($event, item, 'linkKey')">
                    <a-select-option v-for="(option, optionIndex) in superQueryOptions" :key="optionIndex" :value="option.value">
                      {{ option.title }}
                    </a-select-option>
                  </a-select>
                </div>
                <div class="c-o-d-i-1" style="width: 30%">
                  <a-select
                    class="inner-input"
                    :value="item.linkCondition"
                    style="width: 100%"
                    placeholder="请选择查询条件"
                    @change="changeItem($event, item, 'linkCondition')"
                  >
                    <a-select-option value="=">等于</a-select-option>
                    <a-select-option value="min">小于等于</a-select-option>
                    <a-select-option value="max">大于等于</a-select-option>
                    <a-select-option value="like">包含</a-select-option>
                  </a-select>
                </div>
                <div class="c-o-d-i-1" style="width: 30%">
                  <component
                    v-if="item.linkKey && superQueryOptions[item.linkKey].component"
                    :is="superQueryOptions[item.linkKey].component"
                    v-bind="superQueryOptions[item.linkKey].componentProps"
                    v-model:value="item.linkValue"
                  />
                  <a-input
                    v-else
                    class="inner-input"
                    style="width: 100%"
                    :value="item.linkValue"
                    @change="changeItem($event, item, 'linkValue')"
                    placeholder="请输入查询字符"
                  />
                </div>
              </div>
              <div class="c-o-d-o">
                <plus-circle-outlined @click="addCur(index)" class="addBtn" :style="{ color: '#08c', marginRight: '10px' }" />
                <delete-outlined @click="deleteCur(index)" v-if="index !== 0" :style="{ color: 'red' }" />
              </div>
            </div>
          </div>
          <!-- <div class="search-right">
            <Card title="保存的查询">
              <div v-if="QUeryList.length > 0">
                <div v-for="(item, index) in QUeryList" :key="index">
                  <CardGrid style="width: 100%; text-align: center" @click="CardClick(item)">{{ item }}</CardGrid>
                </div>
              </div>
              <div v-else>
                <Empty />
              </div>
            </Card>
          </div> -->
        </div>
      </template>
    </BasicForm>

    <template #footer>
      <div>
        <!--       <a-button type="primary" style="float: left">重置</a-button> -->
        <!-- <a-button type="primary" style="float: left" @click="SaveQuery">保存</a-button> -->
      </div>
      <div style="height: 30px">
        <a-button type="primary" style="float: right; margin-right: 8px" @click="handleSearch">查询</a-button>
        <a-button type="primary" style="float: right; margin-right: 8px" @click="handleClose">关闭</a-button>
      </div>
    </template>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, defineProps, defineEmits } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { superQueryOptions, superQuerySchema } from '../GoodsSearch.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useTableContext } from '/@/components/Table/src/hooks/useTableContext';
  import { Button } from '/@/components/Button';
  import { PlusCircleOutlined, DeleteOutlined } from '@ant-design/icons-vue';
  import { Card, CardGrid, Empty } from 'ant-design-vue';
  import { map } from 'lodash-es';
  import JCascader from '/@/components/Form/src/jeecg/components/JCascader.vue';
  const emits = defineEmits(['handleSubmit', 'handleClose']);
  const { createMessage } = useMessage();
  const table = useTableContext();
  const [registerForm, { resetFields }] = useForm({
    labelWidth: 100,
    baseColProps: { span: 24 },
    schemas: superQuerySchema,
    showActionButtonGroup: false,
    actionColOptions: {
      span: 23,
    },
  });
  let connectArr = ref<Array<{ linkKey: string | null; linkCondition: string | null; linkValue: string | null }>>([
    {
      linkKey: null,
      linkCondition: null,
      linkValue: null,
    },
  ]);

  const pipei = ref('all');
  const props = defineProps({
    title: { type: String, default: '高级查询构造器' },
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });
    console.log(data);

    connectArr.value = [
      {
        linkKey: null,
        linkCondition: '=',
        linkValue: null,
      },
    ];
  });

  /** 添加筛选条件 */
  const addCur = (index: number) => {
    let obj = {
      linkKey: null,
      linkCondition: '=',
      linkValue: null,
    };
    connectArr.value.splice(index + 1, 0, obj);
  };
  /** 删除筛选条件 */
  const deleteCur = (index: number) => {
    connectArr.value.splice(index, 1);
  };
  const changeItem = (e: any, item: any, type: string) => {
    if (type === 'linkKey') {
      item.linkKey = e;
    }
    if (type === 'linkCondition') {
      item.linkCondition = e;
    }
    if (type === 'linkValue') {
      item.linkValue = e.target.value;
    }
  };
  const QUeryList = ref<String[]>([]);
  let maps = new Map();
  async function SaveQuery() {
    maps.set(pipei.value, connectArr.value);
    QUeryList.value.push(pipei.value);
  }

  /** 查询 */
  const handleSearch = async () => {
    const err = connectArr.value.find((item) => {
      if (!item.linkKey || !item.linkCondition) {
        return true;
      }
    });
    if (err) {
      createMessage.warning('请填写完整查询条件');
      return;
    }
    emits('handleSubmit', connectArr.value);
  };
  /** 关闭 */
  const handleClose = async () => {
    emits('handleClose');
    connectArr.value = [
      {
        linkKey: null,
        linkCondition: '=',
        linkValue: null,
      },
    ];
  };

  // function CardClick(val) {
  //   var list = maps.get(val);
  //   connectArr.value = [];
  //   for (let index = 0; index < list.length; index++) {
  //     const element = list[index];

  //     let obj = {
  //       linkKey: element.linkKey,
  //       linkCondition: element.linkCondition,
  //       linkValue: element.linkValue,
  //     };
  //     connectArr.value.splice(index + 1, 0, obj);
  //   }
  // }
</script>
<style lang="less" scoped>
  .search-container {
    display: flex;
    width: 100%;
    height: 100%;
    padding: 12px;

    .search-left {
      width: 100%;
      height: 100%;

      .c-o-d {
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: center;
        justify-content: space-between;
        padding-right: 10px;
        margin-bottom: 10px;
        margin-left: -23px;

        .c-o-d-i {
          flex: 9.5;
          display: flex;
          justify-content: space-between;
          padding-left: 22px;

          .c-o-d-i-1 {
            display: flex;
            align-items: center;
            // margin-right: 5px;
            height: 100%;

            .title {
              width: auto;
              text-align: right;
              display: block;
            }

            .inner-input {
              flex: 1;
            }

            .JselectUser {
              width: 100%;
            }
          }
        }

        .c-o-d-o {
          width: 38px;
          text-align: left;
          // border: 1px solid black;
          margin-left: 10px;
          // margin-bottom: 10px;
        }
      }
    }

    // .search-right {
    //   background: rgb(236, 236, 236);
    //   padding: 30px;
    //   width: 30%;
    //   height: 100%;
    // }
  }
</style>
